package com.xiaoshuidi.cloud.module.member.service.mobilemodifyrecord;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.member.controller.admin.mobilemodifyrecord.vo.MemberMobileModifyRecordPageReqVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.mobilemodifyrecord.vo.MemberMobileModifyRecordPageRespVO;
import com.xiaoshuidi.cloud.module.member.api.mobilemodifyrecord.vo.MobileModifyRecordCreateReqVO;
import com.xiaoshuidi.cloud.module.member.convert.mobilemodifyrecord.MobileModifyRecordConvert;
import com.xiaoshuidi.cloud.module.member.pojo.mobilemodifyrecord.MemberMobileModifyRecord;
import com.xiaoshuidi.cloud.module.member.mapper.mobilemodifyrecord.MemberMobileModifyRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.member.repository.MemberMobileModifyRecordRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 手机号修改记录 服务实现类
 * </p>
 *
 * @author hans.zhang
 * @since 2023-12-08
 */
@Service
public class MemberMobileModifyRecordServiceImpl extends ServiceImpl<MemberMobileModifyRecordMapper, MemberMobileModifyRecord> implements MemberMobileModifyRecordService {

    @Resource
    private MemberMobileModifyRecordRepository memberMobileModifyRecordRepository;

    /**
     * 创建修改手机号记录
     * @param mobileModifyRecordCreateReqVO
     */
    @Override
    public CommonResult<Boolean> createRecord(MobileModifyRecordCreateReqVO mobileModifyRecordCreateReqVO) {
        MemberMobileModifyRecord memberMobileModifyRecord = MobileModifyRecordConvert.INSTANCE.convert(mobileModifyRecordCreateReqVO);
        memberMobileModifyRecordRepository.save(memberMobileModifyRecord);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 分页
     * @param reqVO
     * @return
     */
    @Override
    public PageResult<MemberMobileModifyRecordPageRespVO> getPage(MemberMobileModifyRecordPageReqVO reqVO) {
        LambdaQueryWrapper<MemberMobileModifyRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.between(ObjectUtil.isAllNotEmpty(reqVO.getUserCreateTimeStart(), reqVO.getUserCreateTimeEnd()), MemberMobileModifyRecord::getUserCreateTime, reqVO.getUserCreateTimeStart(), reqVO.getUserCreateTimeEnd());
        wrapper.between(ObjectUtil.isAllNotEmpty(reqVO.getCreateTimeStart(), reqVO.getCreateTimeEnd()), MemberMobileModifyRecord::getCreateTime, reqVO.getCreateTimeStart(), reqVO.getCreateTimeEnd());
        wrapper.and(ObjectUtil.isNotEmpty(reqVO.getSearchBox()), w -> {
            w.like(MemberMobileModifyRecord::getUserName, reqVO.getSearchBox())
                    .or()
                    .like(MemberMobileModifyRecord::getRealName, reqVO.getSearchBox())
                    .or()
                    .like(MemberMobileModifyRecord::getOperator, reqVO.getSearchBox());
        });
        wrapper.orderByDesc(MemberMobileModifyRecord::getCreateTime);
        Page<MemberMobileModifyRecord> page = memberMobileModifyRecordRepository.page(new Page<>(reqVO.getPageNo(), reqVO.getPageSize()), wrapper);
        List<MemberMobileModifyRecordPageRespVO> respVOS = MobileModifyRecordConvert.INSTANCE.convertList(page.getRecords());
        return new PageResult<>(respVOS, page.getTotal());
    }

    /**
     * 导出查询
     * @param reqVO
     * @return
     */
    @Override
    public List<MemberMobileModifyRecordPageRespVO> exportList(MemberMobileModifyRecordPageReqVO reqVO) {
        LambdaQueryWrapper<MemberMobileModifyRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.between(ObjectUtil.isAllNotEmpty(reqVO.getUserCreateTimeStart(), reqVO.getUserCreateTimeEnd()), MemberMobileModifyRecord::getUserCreateTime, reqVO.getUserCreateTimeStart(), reqVO.getUserCreateTimeEnd());
        wrapper.between(ObjectUtil.isAllNotEmpty(reqVO.getCreateTimeStart(), reqVO.getCreateTimeEnd()), MemberMobileModifyRecord::getCreateTime, reqVO.getCreateTimeStart(), reqVO.getCreateTimeEnd());
        wrapper.and(ObjectUtil.isNotEmpty(reqVO.getSearchBox()), w -> {
            w.like(MemberMobileModifyRecord::getUserName, reqVO.getSearchBox())
                    .or()
                    .like(MemberMobileModifyRecord::getRealName, reqVO.getSearchBox())
                    .or()
                    .like(MemberMobileModifyRecord::getOperator, reqVO.getSearchBox());
        });
        wrapper.orderByDesc(MemberMobileModifyRecord::getCreateTime);
        List<MemberMobileModifyRecord> list = memberMobileModifyRecordRepository.list(wrapper);
        return MobileModifyRecordConvert.INSTANCE.convertList(list);
    }
}
